/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.cd.cms.modules.mid.service;

import java.util.List;

import com.cd.cms.common.utils.StringUtils;
import com.cd.cms.modules.mid.entity.GoodInfo;
import com.cd.cms.modules.mid.entity.PlatformInfo;
import com.cd.cms.modules.mid.entity.TorderProduct;
import com.cd.cms.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cd.cms.common.persistence.Page;
import com.cd.cms.common.service.CrudService;
import com.cd.cms.modules.mid.entity.TorderRquest;
import com.cd.cms.modules.mid.dao.TorderRquestDao;

/**
 * 订单Service
 * @author XueD
 * @version 2016-05-10
 */
@Service
@Transactional(readOnly = true)
public class TorderRquestService extends CrudService<TorderRquestDao, TorderRquest> {

	@Autowired
	private TorderProductService torderProductService;

	@Autowired
	private GoodInfoService goodInfoService;

	@Autowired
	private PlatformInfoService platformInfoService;

	public TorderRquest get(String id) {
		return super.get(id);
	}

	public List<TorderRquest> getList(TorderRquest torderRquest) {
		return dao.getList(torderRquest);
	}
	
	public List<TorderRquest> findList(TorderRquest torderRquest) {
		return super.findList(torderRquest);
	}
	
	public Page<TorderRquest> findPage(Page<TorderRquest> page, TorderRquest torderRquest) {
		return super.findPage(page, torderRquest);
	}
	
	@Transactional(readOnly = false)
	public void save(TorderRquest torderRquest) {
		super.save(torderRquest);
	}
	
	@Transactional(readOnly = false)
	public void delete(TorderRquest torderRquest) {
		super.delete(torderRquest);
	}

	@Transactional(readOnly = false)
	public void insertOrder(List<TorderRquest> torderRquestList){
		GoodInfo paramInfo = new GoodInfo();
		paramInfo.setIsvalid("1");
		List<GoodInfo>  goodInfos = goodInfoService.findList(paramInfo);
		PlatformInfo paramPlatformInfo = new PlatformInfo();
		paramInfo.setIsvalid("1");
		List<PlatformInfo> platformInfos = platformInfoService.findList(paramPlatformInfo);
//		UserUtils.getUser().getUserType();
		//平台类型目前默认2
		String platformType = "2";
		for(TorderRquest torderRquest:torderRquestList){
			GoodInfo returnGood = existGood(torderRquest,goodInfos);
			PlatformInfo platformInfo =existPlatformInfo(torderRquest,platformInfos,platformType);
			if(returnGood==null){
				continue;
			}
			this.save(torderRquest);
			TorderProduct torderProduct = new TorderProduct();
			torderProduct.settId(torderRquest.gettId());
			torderProduct.setGoodInfoId(returnGood.getGoodInfoId());
			torderProduct.setgEngName(torderRquest.getgEngName());
			torderProduct.setgChineseName(torderRquest.getgChineseName());
			torderProduct.setnGoodsCount(torderRquest.getnGoodsCount());
			torderProduct.setnPrice(platformInfo.getNPrice());
			torderProduct.setnMount(torderRquest.getnMount());
			if(StringUtils.isEmpty(torderRquest.getnMount())){
				continue;
			}
			if(StringUtils.isEmpty(platformInfo.getNPrice())){
				continue;
			}
			Double count = Double.parseDouble(torderRquest.getnMount());
			Double price = Double.parseDouble(platformInfo.getNPrice());
			Double totalPrice =price*count;
			torderProduct.setnTotalPrice(String.valueOf(totalPrice));
			torderProduct.setPlatformType(platformInfo.getPlatformType());
			torderProductService.save(torderProduct);

		}
	}

	/**
	 * 判断订单产品是否在产品列表中
	 * @param torderRquest
	 * @param goodInfos
     * @return
     */
	private GoodInfo existGood(TorderRquest torderRquest,List<GoodInfo>  goodInfos){
		if(goodInfos==null){
			return null;
		}
		if(goodInfos.size()==0){
			return null;
		}
		for(GoodInfo good:goodInfos){
			if(good.getgEngDesc().equals(torderRquest.getgEngName())){
				return good;
			}
		}
		return null;
	}
	/**
	 * 产品价格是否在列表中
	 * @param torderRquest
	 * @param goodInfos
	 * @return
	 */
	private PlatformInfo existPlatformInfo(TorderRquest torderRquest, List<PlatformInfo>  goodInfos,String platformType){
		if(goodInfos==null){
			return null;
		}
		if(goodInfos.size()==0){
			return null;
		}
		for(PlatformInfo good:goodInfos){
			if((good.getGEngDesc().equals(torderRquest.getgEngName()))&&(good.getPlatformType().equals(platformType))){
				return good;
			}
		}
		return null;
	}
}